<template>
  <div>
    <div class="title-container">
      <h3
        class="title"
        style="color: black; font-weight: bold; text-align: center"
      >
        修改密码
      </h3>
    </div>
    <div style="width: 70%; margin-top: 5%; margin: auto">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原始密码" prop="re_pass">
          <el-input
            placeholder="请输入你的原始密码"
            type="password"
            v-model="ruleForm.re_pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pass">
          <el-input
            placeholder="请输入你的新密码"
            type="password"
            v-model="ruleForm.new_pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkpass">
          <el-input
            placeholder="请再次输入新密码"
            type="password"
            v-model="ruleForm.checkpass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native.prevent="submitForm"
            >确认修改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "countcontrol",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原始密码"));
      } else {
        if (value.length < 6) {
          callback(new Error("密码长度不能低于6"));
        }
        callback();
      }
    };
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新的密码"));
      } else {
        if (value.length < 6) {
          callback(new Error("密码长度不能低于6"));
        }
        // if (this.ruleForm.checkPass !== '' ) {
        //   this.$refs.ruleForm.validateField('checkPass');
        // }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.new_pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {},
      user: localStorage.getItem("user")
        ? JSON.parse(localStorage.getItem("user"))
        : {},
      ruleForm: {
        re_pass: "",
        new_pass: "",
        checkpass: "",
      },
      rules: {
        re_pass: [{ validator: validatePass, trigger: "blur" }],
        new_pass: [{ validator: validatePass1, trigger: "blur" }],
        checkpass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  created() {
    this.form.username = this.user.username;
  },
  methods: {
    // save(){
    //   this.$refs.password.validate((valid) =>{
    //     if(valid){
    //       this.request.post("/user/password",this.form).then(res => {
    //         if(res.code === '200'){
    //           this.$message.success("修改成功")
    //           this.$store.commit("logout")
    //         }else{
    //           this.$message.error(res.msg)
    //         }
    //       })
    //     }
    //   })
    // },

    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/changePwd", this.ruleForm)
            .then(() => {
              this.$store.dispatch("user/resetToken");
              this.$router.push({ path: "/login" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
</style>

